/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Proxy;

import Servidor.DadosObjeto;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;

/**
 *
 * @author Murilo
 */
public class ServidorProxy {

    private int Porta;
    private DatagramSocket socketServidorProxy;
    private static int threads = 0;

    public ServidorProxy(int tamanhoCache) throws SocketException, IOException {
        this.Porta = 4040;
        this.socketServidorProxy = new DatagramSocket(this.Porta);
        CacheProxy.setTamanhoCache(tamanhoCache);
        CacheProxy.setTamanhoDisponivelCache(tamanhoCache);
        System.out.println("Servidor Iniciado");
    }

    public synchronized void iniciaServidorProxy() throws IOException {
        int maxThreads = 3;
        while (true) {
            //cria um novo socket para cada novo cliente
            DatagramPacket requisicaoCliente = new DatagramPacket(new byte[512], 512);
            this.socketServidorProxy.setSoTimeout(100000);
            try{
                this.socketServidorProxy.receive(requisicaoCliente);
                ThreadProxy threadProxy = new ThreadProxy(requisicaoCliente);
                threadProxy.run();
                
                threads++;
                System.out.println("Numero de threads ativas: " + threads);
            }catch(Exception ex){
                System.out.println(ex.getMessage());
            }
        }
    }
}
